package org.ws.edu.task2.commands;

import org.ws.edu.task2.Element;
import org.ws.edu.task2.ICommand;
import org.ws.edu.task2.VirtualFileSystem;
import org.ws.edu.task2.exceptions.ExtensionLengthException;
import org.ws.edu.task2.exceptions.IncorrectPathException;
import org.ws.edu.task2.exceptions.IncorrectSymbolsException;
import org.ws.edu.task2.exceptions.NameLengthException;

public class MakeFolder implements ICommand {
    private String path[] = null;
    private Element element = null;

    public MakeFolder(String path) {
        this.path = path.split("\\\\");
    }

    @Override
    public void execute(VirtualFileSystem vfs) throws IncorrectPathException, ExtensionLengthException, NameLengthException,
            IncorrectSymbolsException {
        validPath(vfs);
        if (vfs.isCurrentFolder(element)) {
            vfs.addElement(new Element(path[path.length - 1], true));
            vfs.changeCurrentElement(element.elements.ceiling(new Element(path[path.length - 1], true)));
        } else {
            throw new IncorrectPathException();
        }
    }

    @Override
    public void validPath(VirtualFileSystem vfs) throws ExtensionLengthException, NameLengthException, IncorrectSymbolsException,
            IncorrectPathException {
        element = vfs.getElementByPath(path);
    }
}
